// JavaScript Document
$(function(){
	
	//计算内容上下padding
	reContPadding({main:"#main",header:"#header",footer:"#footer"});
	function reContPadding(o){
		var main = o.main || "#main",
			header = o.header || null,
			footer = o.footer || null;
		var cont_pt = $(header).outerHeight(true),
			cont_pb = $(footer).outerHeight(true);
		$(main).css({paddingTop:cont_pt,paddingBottom:cont_pb});
	}

    //FastClick.attach(document.body);

});
/**
 * 显示错误信息
 * @param msg
 * @param time
 */
function showFailMsg(msg,time,callback){
    if(!msg){
        msg = "操作失败，请重试"
    }
    if(!time){
        time = 3000;
    }
    var html = '<div class="head-alert head-alert-danger head-alert-dismissible">'+
        '<button type="button" class="head-close" data-dismiss="alert" aria-hidden="true" id="head-message-close">&times;</button>'+msg+'</div>';
    $("#error-message-div").html(html);
    $("#head-message-close").live('click',function(){
        $("#error-message-div").slideUp(400);
	});
    //三秒自动关闭
    setTimeout(function(){
        $("#error-message-div").slideDown(400);
        setTimeout(function(){
            $("#error-message-div").slideUp(400);
            if(callback){
            	callback();
			}
        },time);
    },400);
}

/**
 * 显示成功提示信息
 * @param msg
 * @param time
 */
function showSuccessMsg(msg,time,callback){
    if(!msg){
        msg = "操作成功"
    }
    if(!time){
        time = 3000;
    }
    var html = '<div class="head-alert head-alert-success head-alert-dismissible">'+
        '<button type="button" class="head-close" data-dismiss="alert" aria-hidden="true" id="head-message-close">&times;</button>'+msg+'</div>';
    $("#error-message-div").html(html);
    $("#head-message-close").live('click',function(){
        $("#error-message-div").slideUp(400);
    });
    //三秒自动关闭
    setTimeout(function(){
        $("#error-message-div").slideDown(400);
        setTimeout(function(){
            $("#error-message-div").slideUp(400);
            if(callback){
                callback();
            }
        },time);
    },400);
}

/**
 * 关闭消息提示
 * 请只在消息提示显示时使用
 */
function closeMsg(){
    $("#error-message-div").slideDown(400);
}


/**
 * 返回
 * @param type  1-返回上一个页面 2-返回指定页面 3-返回APP原生
 * @param url
 */
function appBack(type,url){
    if(type == 1){
        //返回上一个页面
        history.go(-1);
    }else if(type == 2){
        //返回到指定页面
        if(url)
        location.href = url;
    }else if(type == 3){
        //mui.toast(JSON.stringify(JKEventHandler));
        //返回原生
        //安卓返回
        if(window.Native){
            window.Native.finish();
        }
        if(window.JKEventHandler){
            //window.JKEventHandler.callNativeFunction('logIOS','返回原生调用开始');
            //iOS返回
            window.JKEventHandler.callNativeFunction('popCurrentView');
            //window.JKEventHandler.callNativeFunction('logIOS','返回原生调用结束');
        }
    }else{
        //返回上一个页面
        history.go(-1);
    }
}

/**
 * 登录（跳转到原生）
 */
function appLogin(){
    //mui.toast("1:"+JSON.stringify(window.Native));
    //安卓
    if(window.Native){
        window.Native.openLoginView();
        return;
    }
    //mui.toast("2:"+JSON.stringify(window.JKEventHandler));
    if(window.JKEventHandler){
       // window.JKEventHandler.callNativeFunction('logIOS','打开登录界面调用开始');
        //ios
        window.JKEventHandler.callNativeFunction('openLoginView');
        //window.JKEventHandler.callNativeFunction('logIOS','打开登录界面调用结束');
        //mui.toast("3:"+JSON.stringify(window.JKEventHandler));
        return;
    }
    if(mui){
        mui.toast("登录失败，请退出APP重试！");
    }
}

/**
 * 打开新窗口
 * tab页面使用
 * @param url
 */
function pushNewWebView(url){
    //安卓
    if(window.Native){
        window.Native.pushNewWebWindow(url);
        return;
    }
    //mui.toast("2:"+JSON.stringify(window.JKEventHandler));
    if(window.JKEventHandler){
        //window.JKEventHandler.callNativeFunction('logIOS','打开新界面调用开始');
        //ios
        window.JKEventHandler.callNativeFunction('pushNewWebView',url);
        //window.JKEventHandler.callNativeFunction('logIOS','打开新界面调用结束');
        return;
    }
    window.location.href = url;
}

/**
 * ajaxLoading动画
 */
function ajaxLoading(){
    if(!window.hearingMask){
        window.hearingMask = mui.createMask();
    }
    $(".hearing-loading").show();
    window.hearingMask.show();
}

/**
 * 关闭ajaxLoading动画
 */
function ajaxLoadingHide(){
    if(!window.hearingMask){
        window.hearingMask = mui.createMask();
    }
    $(".hearing-loading").hide();
    window.hearingMask.close();
}